/* 
 * JavaScript file with Plane Landing System UI Global Functions
 */

/**
 * @name setCurrentAirportGUI
 * @requires Airport Object
 * Shows all the GUI elements of the selected airport.
 */
function setCurrentAirportGUI(airport){
	$('#btn_selected_airport').html(airport.name);
	$('#airport_name').html(airport.name);
	$('#arrival_line_title').html(airport.name+" Radar Arrival Line");
	
	//clean radar for new airport
	$('#arrivals').html('');
	
	//Load tracks according to the airport values;
	$('#landingFieldsContainer').html('');
	var i,value;
	for (i=0; i<airport.numberOfLandingFields; i++){
		value = i+1;
		$('#landingFieldsContainer').append('<div id="trackContainer_'+value+'" class="trackContainer"><div id="track_'+value+'" class="alert track center"></div><div id="title_track_'+value+'" class="title_track">Landing Field '+value+'</div></div>');
	}
}

/**
 * @name addPlane
 * @requires Aircraft Object
 * Creates a new plane in the UI, updates the counter values
 */
function addPlane(aircraft){

   $("#arrivals").append('<li class="plane" id="plane_'+aircraft.id+'">'+
                            '<img class="img-rounded" alt="plane'+aircraft.id+'" src="resources/img/plane.png"><br/>'+
                            '<strong>'+aircraft.id+'</strong>'+
                        '</li>');     
   
   //add draggable functionality
   //activatePlains(); //activate drag-drop function
}

/**
 * @name removePlane
 * @requires Aircraft Id
 * Deletes the last plane on the UI arrival line, updates the counter values
 */
function removePlane(aircraftId){
     $("ul#arrivals li#plane_"+aircraftId).remove();
}

/**
 * @name landPlane
 * @requires Aircraft Object
 * Chooses a landing field for each plane
 */
function landPlane(aircraft){
      var land = false;
      if($("ul#arrivals").children().hasClass('plane')){
         $('[id^="track_"]').each(function(){
             if($(this).is(':empty')){
                 $(this).append('<ul id="landing_'+$(this).attr('id')+'" class="nav nav-pills landing"></ul>');
                 $("#landing_"+$(this).attr('id')).append($("li#plane_"+aircraft.id));
                 $(this).addClass('track_selected');
                 land = true;
                 return false;
             }
         });
         
         if(!land){
             alert('All the Landing Fields are busy. Please wait...'); 
         }
      }
}

/**
 * @name parkPlane
 * @requires Aircraft Object
 * Removes the planes from the landing fields
 */
function parkPlane(aircraft){
   var parking_plane = 'plane_'+aircraft.id; //get the values from controller
   var free_track = $('#'+parking_plane).parent().parent().attr("id"); //get the values from controller

   $('#'+parking_plane).parent().remove();
   $('#'+free_track).removeClass('track_selected');
   
}

/**
 * @name activateTracks
 * Add functionality for accept planes on the tracks
 */
function activateTracks(){
    $(".track").droppable({
        hoverClass: "track_hover",
        drop: function( event, ui ) {
              $(this).addClass( "track_selected" );
        }
    });        
}

/**
 * @name activatePlains
 * Add functionality for moving planes to the tracks
 */
function activatePlains(){
    $('[id^="plane_"]').each(function(){
        $(this).draggable({});
    });
}



